// pages/goodsDetail/goodsDetail.js
var WxParse = require('../../wxParse/wxParse')
const {
  goodsDetail,
  goodsFav,
  checkGoodsFav,
  deleteFav,
  favList,
  cartInfo,
  addCart,
} = require('../../utils/http')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailInfo: {},
    show: false,
    properties: '',
    hasFav: false,
    carLength: 0,
    showAddToCarPopup: false,
    havePropery: false,
    number: 1
  },
  addToCar() {
    const that = this
    const toCarInfo = {}
    const token = wx.getStorageSync('token')
    toCarInfo.token = token
    toCarInfo.goodsId = this.data.detailInfo.basicInfo.id
    toCarInfo.number = this.data.number
    console.log(toCarInfo)
    addCart(toCarInfo).then(res => {
      console.log(res)
      that.setData({
        showAddToCarPopup:false
      })
      wx.showToast({
        title: '加入购物车成功',
        icon: 'success'
      })
    })
  },
  // addToCar(e){
  //   const id = e.currentTarget.dataset.operation
  //   const token = wx.getStorageSync('token')
  //   const toCarInfo = {}
  //   toCarInfo.goodsId = id
  //   toCarInfo.number = 1
  //   toCarInfo.token = token
  //   addCart(toCarInfo).then(res=>{
  //       console.log(res)
  //   })
  //   wx.showToast({
  //     title: '加入购物车成功',
  //     icon: 'success'
  //   })
  // },
  onChange(event) {
    console.log(event.detail);
    this.setData({
      number: event.detail
    })
  },
  showPopup() {
    this.setData({
      show: !this.data.show
    })
  },
  onClose() {
    this.setData({
      show: !this.data.show
    })
  },
  showAddToCarPopup() {
    this.setData({
      showAddToCarPopup: true
    })
  },
  addFav() {
    const that = this
    const goodsId = this.data.detailInfo.basicInfo.id
    var token = wx.getStorageSync('token')
    const queryInfo = {}
    queryInfo.goodsId = goodsId
    queryInfo.token = token
    goodsFav(queryInfo).then(res => {
      that.setData({
        hasFav: !that.data.hasFav
      })
      console.log(res)
    })
  },
  deleteFav() {
    const that = this
    const goodsId = this.data.detailInfo.basicInfo.id
    var token = wx.getStorageSync('token')
    console.log(token)
    const queryInfo = {}
    queryInfo.goodsId = goodsId
    queryInfo.token = token

    favList({ token: token }).then(res => {
      res.data.forEach(item => {
        if (item.goodsId == goodsId) {
          queryInfo.id = item.id
        }
      })
    })
    console.log(queryInfo)

    deleteFav(queryInfo).then(res => {
      console.log(res)
      that.setData({
        hasFav: !that.data.hasFav
      })
    })

  },
  toCar() {
    // wx.navigateTo({
    //   url: '/pages/car/car'
    // })
    wx.reLaunch({
      url: '/pages/car/car'
    });
  },
  getCarLength() {
    var length = 0
    const that = this
    const token = wx.getStorageSync('token')
    cartInfo(token).then(res => {
      res.data.items.forEach(item => {
        length += item.number
      })
      that.setData({
        carLength: length
      })
      console.log(that.data.carLength)
    })
  },
  closeAddToCarPopup() {
    this.setData({
      showAddToCarPopup: !this.data.showAddToCarPopup
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCarLength()
    var that = this
    // console.log(options.id)
    var token = wx.getStorageSync('token')
    const queryInfo = {}
    queryInfo.token = token
    const propertiesName = []
    goodsDetail(options.id).then(res => {
      // res.data.properties.forEach(item=>{
      //   propertiesName.push(item.name)
      // })
      var isHave = false
      if (res.data.properties) {
        isHave = true
      } else {
        isHave = false
      }
      this.setData({
        havePropery: isHave
      })
      var goodsId = res.data.basicInfo.id
      // console.log(goodsId)
      queryInfo.goodsId = goodsId
      //检查商品是否已被收藏
      checkGoodsFav(queryInfo).then(resC => {
        // console.log(resC)
        if (resC.code == 0) {
          that.setData({
            hasFav: true,
          })
        }
      })
      this.setData({
        detailInfo: res.data,
        properties: propertiesName.join(' ')
      })
      console.log(this.data.detailInfo)
      var article = res.data.content;
      var that = this;
      WxParse.wxParse('article', 'html', article, that, 5);
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})